package com.gwclh.personnel.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.entity.Internship;
import com.gwclh.personnel.entity.User;
import com.gwclh.personnel.service.IEmployeeService;
import com.gwclh.personnel.service.IInternshipService;
import com.gwclh.personnel.utils.ShiroUtil;
import com.gwclh.personnel.vo.internshipvo.InternshipEmpVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实习表，说明，若要查询实习员工，通过查询状态为0与2的emp_id，若查询已转正员工，则查询状态为1 前端控制器
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@RestController
@RequestMapping("/internships")
public class InternshipController extends BaseController {
    @Autowired
    IInternshipService internshipService;

    @Autowired
    IEmployeeService employeeService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取实习时间")
    @ApiImplicitParam(name = "id",value = "员工id",dataType = "Integer",paramType = "path",required
            = true)
    public ResultVo getInternshipById(@PathVariable(value = "id") Integer id){
//        Internship internship = internshipService.getById(id);
        Internship internship = internshipService.getOne(new QueryWrapper<Internship>().eq("emp_id", id));
        Assert.notNull(internship, "没有实习经历");
        return ResultVo.success(internship);
    }

    @GetMapping("")
    @ApiOperation(value = "分页查询实习员工列表")
    public ResultVo listInternship(InternshipEmpVo internshipEmpVo, HttpServletRequest request){
        PageDomain pageDomain = getPageParams(request);
        return internshipService.listInternship(pageDomain,internshipEmpVo);
    }
    @GetMapping("/turn")
    @ApiOperation(value = "分页查询已转正员工列表")
    public ResultVo listTurnRightInternship(InternshipEmpVo internshipEmpVo, HttpServletRequest request){
        PageDomain pageDomain = getPageParams(request);
        return internshipService.listTurnRightInternship(pageDomain,internshipEmpVo);
    }

    @PutMapping("")
    @ApiOperation(value = "更新试用期员工信息")
    public ResultVo updateInternship(@RequestBody Internship internship){
        internship.setOperId(ShiroUtil.getProfile().getId());
        if(internship.getInternStatus() == 2){
            internship.setEndDate(internship.getOperDate());
        }
        Assert.isTrue(internshipService.updateById(internship),"操作考核失败");

        // 更改员工信息

        if(internship.getInternStatus() == 1){
            UpdateWrapper<Employee> employeeUpdateWrapper = new UpdateWrapper<>();
            employeeUpdateWrapper.eq("id",internship.getEmpId()).set("is_internship",0);
            employeeService.update(employeeUpdateWrapper);
        }
        if(internship.getInternStatus() == 3){

        }


        return ResultVo.success();
    }

    @PostMapping("/index")
    @ApiOperation(value = "试用期员工列表")
    public ResultVo getInternship(){
        IPage<Map<String, Object>> internship = internshipService.getInternship();
        Assert.notNull(internship,"试用期员工为空");

        System.out.println("试用期员工列表");
        return ResultVo.success(internship);
    }

    @PostMapping("/search")
    @ApiOperation("根据条件查询试用期员工")
    public ResultVo getInternshipBySearch(Integer empId, String empName
            , String deptName, String staName, Integer internStatus
            , Date startDate, Date endDate){
        IPage<Map<String, Object>> internshipBySearch = internshipService.getInternshipBySearch(empId, empName, deptName, staName, internStatus, startDate, endDate);
        Assert.notNull(internshipBySearch,"根据此条件未查询到试用期员工");
        return ResultVo.success(internshipBySearch);
    }

    @PostMapping("/selectFormal")
    @ApiOperation(value = "查询已转正员工")
    public ResultVo selectFormal(){
        IPage<Map<String, Object>> internshipIPage = internshipService.selectFormal();
        Assert.notNull(internshipIPage,"未查询到已转正员工");
        return ResultVo.success(internshipIPage);
    }

    @PostMapping("/selectFormalBySearch")
    @ApiOperation("根据条件查询转正员工")
    public ResultVo selectFormalBySearch(Integer empId, String empName
            , String deptName, Date startDate, Date endDate){
        IPage<Map<String, Object>> internshipIPage = internshipService.selectFormalBySearch(empId,empName,deptName,startDate,endDate);
        Assert.notNull(internshipIPage,"根据条件未查询到转正员工");
        return ResultVo.success(internshipIPage);
    }

    @PostMapping("/beFormal/{id}/{operate}")
    @ApiOperation(value = "员工转正,延迟,不录用操作")
    public ResultVo operate(@PathVariable("id") Integer id
                            ,@PathVariable("operate") Integer operate
                            , String comment
                            , Date operDate
                            , String remark
                            , Integer operId
                            ){
        Integer result = internshipService.operate(id, operate, comment, operDate, remark, operId);
        Assert.notNull(result,"转正，延迟或不录用操作失败");
        return ResultVo.success("操作成功");
    }

}
